﻿<?php
/*
 * @Author Akos Szabo - Axi
 * Created on Jul 5, 2011
 * @Project: ujej
 * @Description: This class is for handling database actions
 * The root of this class was found on the web:
 * http://phpbuilder.com/snippet/detail.php?type=snippet&id=613
 * Origin author: khaine
 
This class holds all the functions needed for basic mysql database access.

functions within this class are.

    the constructor. mysql_dbase which is called with database name, hostname, username, and password. or they can be set to be defaults, The dbname default has to be left as test for internal reasons. this means when creating a new instance of this class the minimum call is normally: $inst=new mysql_dbase("dbname"); unless you are creating a database. in which you can leave the name blank.

All functions will halt processing and display an error message in the browser on a fault in any of the commands.

    connect()			: no options just creates the connection 
 				  to the database.
    use_db(name)		: Change the current database to the one named.
    make_db(name)		: Creates a new database with the given name.
    drop_db(name)		: Drops the database with the given name.
				  Note: this function does not seek confirmation
				  and is none recoverable.
    do_query(query)		: Performs the given query and will return the
				  number of rows affected or returned for each
				  query.
    get_data(type)		: Returns an array of data base on the type
     				  supplied. A is associative, N is numeric, or
				  B for both.
    close_db() 			: Closes the database connection.

*/

class mysql_dbase
{
    var $dbname;		// name of database to connect to
    var $hostname;		// Host name of the database
    var $uname;			// Username to connect with
    var $pword;			// password to use
    var $conn;			// Used internally to track a connection
    var $result;   		// Used internally to track a query
    var $errMsg;
	
    function mysql_dbase($dbname="ujej", $hostname="localhost", $username="ujej", $password="Ujej123") 
    {
	$this->dbname=$dbname;
        $this->hostname=$hostname;
        $this->uname=$username;
        $this->pword=$password;
    } 

    function connect()
    {
        $this->conn=mysql_connect($this->hostname, $this->uname, $this->pword);
        if ( !$this->conn )
        {
	    	echo "<BR>\nConnection to database ".$dbname." has failed.<P>\n";
            echo mysql_error();
            exit;
        }

        if ( $this->dbname != "test" )
        {
            $result=mysql_select_db($this->dbname, $this->conn);
            if ( !$result )
            {
                echo "Failed to select database ".$this->dbname."<P>\n";
                echo mysql_error();
                exit;
            }
        }

    }

    function use_db($dbname) 
    {
	if ( $dbname == "" )
	{
	    echo "ERROR : No database name supplied to use_db";
	    exit;
	}
        $result=mysql_select_db($dbname, $this->conn);
        if ( !$result )
        {
	    echo "Failed to select database ".$dbname."<P>\n";
	    echo mysql_error();
	    exit;
        }
        $this->dbname=$dbname;
    }

    function make_db($dbname)
    {
	if ( $dbname == "" )
	{
	    echo "ERROR : No database name supplied to make_db";
	    exit;
	}
	$result=mysql_create_db($dbname, $this->conn);
	if ( !$result )
	{
	    echo "Failed to create database ".$this->dbname."<P>\n";
	    echo mysql_error();
	    exit;
	    
	}
	$this->dbname=$dbname;
	$result=mysql_select_db($this->dbname, $this->conn);
	if ( !$result )
	{
	    echo "Failed to select database ".$this->dbname."<P>\n";
	    echo mysql_error();
	    exit;
	}
	return(1);
    }

    function drop_db($dbname)
    {
	if ( $dbname == "" )
	{
	    echo "ERROR : No database name supplied to drop_db";
	    exit;
	}
	$result=mysql_drop_db($dbname, $this->conn);
	if ( !$result )
	{
	    echo "Failed to drop database ".$dbname."<P>\n";
	    echo mysql_error();
	    exit;
	    
	}
	return(1);
    }

    function do_query($query, $num="n")
    {
	 if ( $query == "" )
	 {
	     echo "ERROR : No query supplied to do_query";
	     exit;
	 }
         $this->result=mysql_query($query, $this->conn);
         if ( !$this->result )
         {
             echo "Query ".$query." on database ".$this->dbname." has failed<P>\n";
	     echo mysql_error();
             exit;
         }      
         $retval=0;  
	 if ( (stristr($query, "delete")) || (stristr($query, "insert")) || (stristr($query, "update")) )
         {
             $retval=mysql_affected_rows($this->conn);
         }
         else
         {
	     $retval=mysql_num_rows($this->result);
         } 
	 return($retval);
    }



    function get_data($type="B")
    {
        if ( $type == "A" )
        {
	    $fields=mysql_fetch_array($this->result, MYSQL_ASSOC);
        }
        elseif ( $type == "N" )
        {
 	    $fields=mysql_fetch_array($this->result, MYSQL_NUM);
        }
        else
        {
	    $fields=mysql_fetch_array($this->result);
        }
        return($fields);
    }

    function close_db()
    {
        $result=mysql_close($this->conn);
        if ( !$result )
        {
            echo "Failed to close database - ".$this->dbname."<P>\n";
	    echo mysql_error();
        }
    }
    
    function getLastAutoId()
    {
    	return mysql_insert_id();
    }
    
    function isMailUsed( $argMail )
    {
    	echo "isMailUsed ENT; "; 
    	$query = "SELECT count(*) FROM users WHERE mail='$argMail'";
    	
    	$res=mysql_query($query, $this->conn);
    	$fields=mysql_fetch_array($res);
    	
    	$resNum = $fields[0]; 
    	 	
    	 	
        if( $resNum==0)
           	return false;
        else
        	return true;	
    }
    
    function getErrMsg()
    {
    	return $errMsg;
    }
    
}//end of class

?> 